//
//  MineHistoryViewController.m
//  huashiedu
//
//  Created by oceans on 2018/8/31.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "MineHistoryViewController.h"
#import "ClassListTableViewCell.h"
#import "HttpRequest.h"
#import <UIImageView+WebCache.h>
#import "ClassModel.h"
#import "NormalWebViewController.h"
#import <MJRefresh.h>

@interface MineHistoryViewController () <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) UITableView *tableview;
@property (strong, nonatomic) NSMutableArray <ClassModel *> *dataArr;
@property (strong, nonatomic) MetaModel *model;
@end

@implementation MineHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupViews];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupViews {
    
    self.navigationItem.title = @"浏览历史";
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight - XNavigationHeight) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorInset = UIEdgeInsetsMake(0, 11, 0, 11);
    [_tableview registerNib:[UINib nibWithNibName:@"ClassListTableViewCell" bundle:nil] forCellReuseIdentifier:@"ClassListTableViewCell"];
    [self.view addSubview:_tableview];
    _tableview.tableFooterView = [UIView new];
    MJRefreshAutoStateFooter *footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    footer.automaticallyHidden = NO;
    _tableview.footer = footer;
}

- (void)loadData {
    
    self.dataArr = [NSMutableArray array];
    __weak typeof(self) weakself = self;
    [HttpRequest getURL:kmine_history paras:nil showProgress:NO successful:^(id  _Nullable result) {
        for (NSDictionary *dic in result[@"data"][@"items"]) {
            ClassModel *model = [[ClassModel alloc] initWithDic:dic];
            [weakself.dataArr addObject:model];
        }
        [weakself.tableview reloadData];
        MetaModel *model = [[MetaModel alloc] initWithDic:result[@"data"][@"meta"]];
        if (model.currentPage == model.pageCount || model.pageCount == 0) {
            [weakself.tableview.footer endRefreshingWithNoMoreData];
        }else {
            weakself.model = model;
        }
    } failed:nil];
}

- (void)loadMoreData {
    self.dataArr = [NSMutableArray array];
    __weak typeof(self) weakself = self;
    [HttpRequest getURL:kmine_history paras:@{@"page": [NSNumber numberWithInt:self.model.currentPage + 1]} showProgress:NO successful:^(id  _Nullable result) {
        for (NSDictionary *dic in result[@"data"][@"items"]) {
            ClassModel *model = [[ClassModel alloc] initWithDic:dic];
            [weakself.dataArr addObject:model];
        }
        [weakself.tableview reloadData];
        MetaModel *model = [[MetaModel alloc] initWithDic:result[@"data"][@"meta"]];
        if (model.currentPage == model.pageCount || model.pageCount == 0) {
            [weakself.tableview.footer endRefreshingWithNoMoreData];
        }else {
            weakself.model = model;
            [weakself.tableview.footer endRefreshing];
        }
    } failed:^(int code, NSString *errMsg) {
        [weakself.tableview.footer endRefreshing];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    const CGFloat height = (ScreenWidth - 20.0) * 0.39 * 0.586 + 32;
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identity = @"ClassListTableViewCell";
    ClassListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity forIndexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(ClassListTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ClassModel *model = _dataArr[indexPath.row];
    [cell.classImageView sd_setImageWithURL:[NSURL URLWithString:model.thumb]];
    cell.titleLabel.text = model.name;
    cell.teacherLabel.text = model.teacher;
    cell.timeLabel.text = model.begintime;
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"￥ " attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:1.0 green:0 blue:54/255.0 alpha:1.0], NSFontAttributeName: [UIFont systemFontOfSize:10.0]}];
    NSAttributedString *price = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f", model.price] attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12.0]}];
    [string appendAttributedString:price];
    cell.priceLabel.attributedText = string;
    cell.stateImageView.hidden = !model.finished;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NormalWebViewController *controller = [[NormalWebViewController alloc] init];
    controller.url = _dataArr[indexPath.row].link;
    [self.navigationController pushViewController:controller animated:YES];
}

@end
